Vue3 Beginner's Notes 3 ---- Using Element Plus to Partition the Page Layout and Implement the Left-Side Common Menu
This series of notes will focus on how to set up a project using Vue3 and interact with the backend API. It won’t cover the basic features of Vue. For the basic features of Vue, you can refer to this video Get Started with Vue in Four Hours. I got started with Vue by watching this video and thought it was pretty good.
Code address: https://github.com/yexia553/vue_study/tree/%E9%85%8D%E7%BD%AEvue-router/vue3-notes
Page Layout Description and Effect Demonstration
Every website has its own layout. The so - called layout is about what information is displayed in which area. The following picture shows the layout effect we designed, which is divided into three parts.
The left side of the page is a whole, used to display the menu bar, that is, to show what modules the website has. The right side is divided into upper and lower parts. The upper part is used to display the user’s avatar. Clicking on the avatar can perform operations such as logging out. The lower part on the right is the main area of the website, displaying the information we want to convey to the users.
Since the left - side menu bar and the upper - right part are needed on every page, we can make these two parts into common components. We can name the left - side menu bar CommonAside, and the upper - right part CommonHeader.
Introduction to Element Plus
People who know Vue basically know Element UI (used with Vue2) or Element Plus. Their function is to encapsulate a lot of UI libraries for us to use directly. For example, if we want to implement a table, we can use the el - table tag to help us achieve it.
Implementing the Layout
1. Installation
To use Element Plus, we need to install it first. Execute the following command in the root directory of the project:
1 | npm install element-plus --save |
Since we will also use the icons in Element Plus later, we install them together here:
1 | npm install @element-plus/icons-vue |
2. Importing
After installation, we need to import Element Plus into Vue3 to use it. We adopt the global import method. In fact, there are several ways to import it. You can click here to see what specific import methods there are.
The import method is configured in main.js. After configuration, main.js is as follows. I have noted in the code which codes are required for the import.
1 | import { createApp } from 'vue' |
1 | #app { |
I won’t explain the CSS part in detail. The main purpose is to enable Vue to display our page correctly in the browser. If you don’t understand it, you need to learn a little bit about CSS.
Implementing the Layout
In the previous note about routes, we wrote the following in Main.vue:
1 | <template> |
We now replace the content inside with the following part:
1 |
|
After replacement, everyone execute npm run dev
in the root directory of the project to run the project. You should be able to see the following page:
As you can see, although it is very simple, the overall framework of the page has come out.
CommonAside Component
As mentioned above, we can extract the left - side menu bar into a component, called CommonAside. Let’s implement it now.
Creating the CommonAside.vue File
First, delete the content automatically created when the project was initialized in src/components.
Create the CommonAside.vue file in the src/components path. The content of the file is as follows:
1 | <template> |
The code is divided into three parts: template, script, and style. Let’s explain them separately.
Template
In the template, we use the el - menu tag to achieve the style we need. In fact, we only use a very simple part here. There are many methods and attributes that we haven’t used. You can take a closer look at the official website of Element Plus.
Script
The script contains JavaScript code. Vue3 supports both the Options API and the Composition API. I use the Composition API. Regarding the differences between the two and how to choose, you can refer to here
In the script, I actually just defined and returned a list containing the content of the left - side menu.
Style
The style contains styles, that is, CSS. But I use less. Regarding the part of less, you can refer to here. In fact, if you don’t know less, it doesn’t matter. Just look at the above code and imitate it. I don’t really know less either, I can only imitate.
Using the CommonAside.vue Component
As analyzed before, the CommonAside component will appear on all pages. So we can import this component in Main.vue. After import, the content of Main.vue is as follows:
1 | <template> |
The content of the code is very simple. I’ll just explain a small knowledge point in the style.
.common-layout means that the style in the following curly braces will take effect within the range of class=”common-layout”.
We will see &>.el-container under.common-layout, which is related to the el - container inside common - layout. If there is also an el - container outside.common-layout, it will not take effect.
Up to this point, the CommonAside component is also completed. After starting the project, the page should be as follows: